import constans from '../../../../assets/utils/Constans.js';
import DateUtil from '../../../../assets/utils/DateUtil.js';
import StringUtil from '../../../../assets/utils/StringUtil.js';
import address from '../../../../assets/json/address.json';
import typeEnum from '../../../../assets/js/dictinfo.js'

export default {
  created: function () {
    var self = this;
    self.readLoad(self);
  },
  watch: {
    '$route': function (route) {
      var self = this;
      self.readLoad(self);
    },
  },
  data(){
    return {
      queryParams : {
        srType : '',
        province : '',
        city : '',
        area : '',
        street : '',
        pushState : '',
        label : '',
      },
      loading : false,
      params : {},
      labels : [],
      label : '',
      srId : '',
      provinces : Object.keys(address),
      citys : [],
      areas : [],
      streets : [],
      pushStateList : [
        {
          label : '发布中',
          value : '0'
        },
        {
          label : '已下架',
          value : '1'
        },
        {
          label : '强制下架',
          value : '2'
        },
        {
          label : '过期发布',
          value : '3'
        }
      ],
      pageNum : 1,
      pageSize : 10,
      totalDataNumber: 0,
      pageSizesList: [10, 15, 20, 30, 50],
      tableData: [],
    }
  },
  methods: {
    onProvinceChange() {
      this.queryParams.city = '';
      this.queryParams.area = '';
      this.queryParams.street = '';
      if (this.queryParams.province == null || this.queryParams.province == '') {
        this.citys = [];
        this.areas = [];
        this.streets = [];
      } else {
        this.citys = Object.keys(address[this.queryParams.province]);
      }
    },
    onCityChange() {
      this.queryParams.area = '';
      this.queryParams.street = '';
      if (this.queryParams.city == null || this.queryParams.city == '') {
        this.areas = [];
        this.streets = [];
      } else {
        this.areas = Object.keys(address[this.queryParams.province][this.queryParams.city]);
      }
    },
    onAreaChange() {
      this.queryParams.street = '';
      if (this.queryParams.area == null || this.queryParams.area == '') {
        this.streets = [];
      } else {
        this.streets = address[this.queryParams.province][this.queryParams.city][this.queryParams.area];
      }
    },
    readLoad (self) {
      self.queryParams = {
        srType : '',
        province : '',
        city : '',
        area : '',
        street : '',
        pushState : '',
        label : ''
      };
      self.query();
    },
    exportExcel () {
      this.$confirm('可能花费较长时间, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let self = this;
        self.$http.get(constans.exportExcel , {responseType: 'arraybuffer', params: self.params}).then((response) =>{
          var blob = new Blob([response.data], {type: "application/vnd.ms-excel"});
          var objectUrl = URL.createObjectURL(blob);
          var a = document.createElement('a');
          var fileName = "xxx.xls";
          if (self.queryParams.srType == 1) {
            fileName = "求租信息.xls";
          } else if (self.queryParams.srType == 2) {
            fileName = "出租信息.xls";
          } else if (self.queryParams.srType == 3) {
            fileName = "求购信息.xls";
          } else if (self.queryParams.srType == 4) {
            fileName = "出售信息.xls";
          }
          document.body.appendChild(a); //此处增加了将创建的添加到body当中
          a.href = objectUrl;
          a.download = fileName;
          a.target = '_blank';
          a.click();
          a.remove(); //将a标签移除
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    formatLabel (row, column, cellValue) {
      return cellValue.substring(0, cellValue.length - 1);
    },
    formatPushTime (row, column, cellValue) {
      return DateUtil.dateFormat("yyyy-MM-dd hh:mm:ss", new Date(cellValue));
    },
    tagClose (label) {
      this.labels.splice(this.labels.indexOf(label), 1);
    },
    query () {
      var self = this;
      self.queryParams.srType = typeEnum[this.$route.params.srType];
      self.params = StringUtil.cloneObject(self.queryParams);
      self.loadTable(self);
    },
    loadTable (self) {
      self.loading = true;
      self.$http.get(constans.querySaleRent + "?pageNum=" + self.pageNum + "&pageSize=" + self.pageSize, {params:self.params}).then((response) => {
        var result = response.data;
        self.tableData = result.tableData;
        self.totalDataNumber = result.total;
        self.loading = false;
      },(response) => {
        self.showToast("服务器已关闭！");
      });
    },
    forciblyUp (srId) {
      var self = this;
      this.$confirm('确认强制下架?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.$http.post(constans.forciblyUp + "?srId=" + srId).then((response) => {
          var result = response.data;
          if (result.messageCode === 200) {
            self.loadTable(self);
            this.$message({
              type: 'success',
              message: '操作成功'
            });
          } else {
            self.showToast(result.message, 2, 1500);
          }
        },(response) => {
          self.showToast("服务器已关闭！");
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    addLabel () {
      var self = this;
      self.label = self.label.trim();
      if (self.label !== null && self.label !== '') {
        if (self.labels.length >= 5) {
          this.$message({
            type: 'warning',
            message: '标签数量已达上限'
          });
          return;
        }
        if (self.label.indexOf("、") >= 0) {
          this.$message({
            type: 'warning',
            message: '标签中不能含有[、]关键字'
          });
          return;
        }
        if (self.labels.indexOf(self.label) >= 0) {
          this.$message({
            type: 'warning',
            message: '标签已存在'
          });
          return;
        }
        self.labels.push(self.label);
      }
      var labelStr = self.labels.join("、") === '' ? self.labels.join("、") : self.labels.join("、") + "、";
      self.$http.post(constans.addLabel + "?srId=" + self.srId + "&label=" + labelStr).then((response) => {
        var result = response.data;
        if (result.messageCode === 200) {
          self.loadTable(self);
          this.$message({
            type: 'success',
            message: '操作成功'
          });
          self.closed("labelModal");
        } else {
          self.showToast(result.message, 2, 1500);
        }
      },(response) => {
        self.showToast("服务器已关闭！");
      });
    },
    openAddLabel (label, srId) {
      var self = this;
      if (label !== null && label !== '') {
        self.labels = label.substring(0, label.length - 1).split("、");
      } else {
        self.labels = [];
      }
      if (srId !== null && srId !== '') {
        self.srId = srId;
      } else {
        self.srId = '';
      }
      self.label = '';
      self.toOpenModal('labelModal');
    },
    handleSizeChange (val) {
      var self = this;
      self.pageSize = val;
      self.loadTable(self);
    },
    handleCurrentChange (val) {
      var self = this;
      self.pageNum = val;
      self.loadTable(self);
    }
  }
}
